package co.yixiang.yshop.module.crm.controller.admin.crmcloudcall;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.common.util.object.BeanUtils;
import co.yixiang.yshop.framework.redis.util.redis.RedisUtil;
import co.yixiang.yshop.module.crm.controller.admin.crmcloudcall.vo.CloudSetVO;
import co.yixiang.yshop.module.crm.controller.admin.crmcloudcall.vo.CrmCloudcallPageReqVO;
import co.yixiang.yshop.module.crm.controller.admin.crmcloudcall.vo.CrmCloudcallRespVO;
import co.yixiang.yshop.module.crm.controller.admin.crmcloudcall.vo.CrmCloudcallSaveReqVO;
import co.yixiang.yshop.module.crm.dal.dataobject.crmcloudcall.CrmCloudcallDO;
import co.yixiang.yshop.module.crm.service.crmcloudcall.CrmCloudcallService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 云呼坐席管理")
@RestController
@RequestMapping("/crm/cloudcall")
@Validated
public class CrmCloudcallController {

    @Resource
    private CrmCloudcallService cloudcallService;

    @PostMapping("/create")
    @Operation(summary = "创建云呼坐席管理")
    @PreAuthorize("@ss.hasPermission('crm:cloudcall:create')")
    public CommonResult<Long> createCloudcall(@Valid @RequestBody CrmCloudcallSaveReqVO createReqVO) {
        return success(cloudcallService.createCloudcall(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新云呼坐席管理")
    @PreAuthorize("@ss.hasPermission('crm:cloudcall:update')")
    public CommonResult<Boolean> updateCloudcall(@Valid @RequestBody CrmCloudcallSaveReqVO updateReqVO) {
        cloudcallService.updateCloudcall(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除云呼坐席管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:cloudcall:delete')")
    public CommonResult<Boolean> deleteCloudcall(@RequestParam("id") Long id) {
        cloudcallService.deleteCloudcall(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得云呼坐席管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:cloudcall:query')")
    public CommonResult<CrmCloudcallRespVO> getCloudcall(@RequestParam("id") Long id) {
        CrmCloudcallDO cloudcall = cloudcallService.getCloudcall(id);
        return success(BeanUtils.toBean(cloudcall, CrmCloudcallRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得云呼坐席管理分页")
    @PreAuthorize("@ss.hasPermission('crm:cloudcall:query')")
    public CommonResult<PageResult<CrmCloudcallRespVO>> getCloudcallPage(@Valid CrmCloudcallPageReqVO pageReqVO) {
        return success(cloudcallService.getCloudcallPage(pageReqVO));
    }


    @GetMapping("/getRedisSet")
    @Operation(summary = "获得云呼叫配置")
    public CommonResult<CloudSetVO> getRedisSet() {
        CloudSetVO cloudSetVO = CloudSetVO.builder()
                .cloudAccountId(RedisUtil.get("cloudAccountId"))
                .cloudSecret(RedisUtil.get("cloudSecret"))
                .build();
        return success(cloudSetVO);
    }

    @PostMapping("/saveRedisSet")
    @Operation(summary = "云呼叫配置")
    public CommonResult<Boolean> saveRedisSet(@Valid @RequestBody CloudSetVO cloudSetVO) {
        RedisUtil.set("cloudAccountId",cloudSetVO.getCloudAccountId());
        RedisUtil.set("cloudSecret",cloudSetVO.getCloudSecret());
        return success(true);
    }

    @GetMapping("/call")
    @Operation(summary = "云呼叫")
    public CommonResult<Boolean> doCloudcall(@RequestParam("id") Long id,
                                             @RequestParam("source") String source,
                                             @RequestParam("phone") String phone) {
       cloudcallService.doCall(id,source,phone);
        return success(true);
    }



}